package com.yougou.ui.order;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.ui.order.adapter.OrderAdapter;
import com.yougou.utils.ToastUtils;

public class OrderListActivity extends BaseActivity {
    
    public static final String EXTRA_ORDER_TYPE = "order_type";
    
    private ImageView mBtnBack;
    private TabLayout mTabLayout;
    private RecyclerView mRecyclerView;
    private View mEmptyLayout;
    
    private OrderAdapter mAdapter;
    private OrderViewModel mViewModel;
    private int mOrderType;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        
        mOrderType = getIntent().getIntExtra(EXTRA_ORDER_TYPE, 1);
        
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mBtnBack = findViewById(R.id.iv_back);
        mTabLayout = findViewById(R.id.tab_layout);
        mRecyclerView = findViewById(R.id.rv_orders);
        mEmptyLayout = findViewById(R.id.layout_empty);
        
        // 返回按钮
        mBtnBack.setOnClickListener(v -> finish());
        
        // 设置Tab
        mTabLayout.addTab(mTabLayout.newTab().setText("全部"));
        mTabLayout.addTab(mTabLayout.newTab().setText("待付款"));
        mTabLayout.addTab(mTabLayout.newTab().setText("待发货"));
        mTabLayout.addTab(mTabLayout.newTab().setText("待收货"));
        mTabLayout.addTab(mTabLayout.newTab().setText("已完成"));
        
        // 设置默认选中Tab
        if (mOrderType >= 0 && mOrderType <= 4) {
            mTabLayout.getTabAt(mOrderType).select();
        }
        
        // Tab切换监听
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewModel.loadOrders(tab.getPosition());
            }
            
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });
        
        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new OrderAdapter();
        mRecyclerView.setAdapter(mAdapter);
        
        // 订单点击事件
        mAdapter.setOnItemClickListener(order -> {
            android.content.Intent intent = new android.content.Intent(this, OrderDetailActivity.class);
            intent.putExtra(OrderDetailActivity.EXTRA_ORDER_NUMBER, order.getOrderNumber());
            startActivityForResult(intent, 100);
        });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            // 订单详情页返回，刷新订单列表
            if (mViewModel != null) {
                mViewModel.loadOrders(mOrderType);
            }
        }
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(OrderViewModel.class);
        
        // 观察订单列表
        mViewModel.getOrderList().observe(this, orders -> {
            if (orders != null && !orders.isEmpty()) {
                mAdapter.setData(orders);
                showOrders();
            } else {
                showEmpty();
            }
        });
        
        // 观察错误信息
        mViewModel.getError().observe(this, error -> {
            if (error != null) {
                ToastUtils.show(this, error);
            }
        });
        
        // 加载订单数据
        mViewModel.loadOrders(mOrderType);
    }
    
    
    private void showEmpty() {
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    }
    
    
    private void showOrders() {
        mEmptyLayout.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
    }
}

